<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {
    
    function __construct() {
        parent::__construct();
        $this->load->model('novel_model');
        $this->load->model('category_model');
    }
    
    public function index() {
        $cached = array();
        
        // Get newest novel 
        $topNovel = $this->novel_model->getNovel_newest(0, 12);
        
        // Get Last update chapter 
        $update = $this->novel_model->getChapter_lastest();
        foreach($update as &$n) {
            $arr = array();
            $novelId = $n['novelId'];
            if(!in_array('n_' . $novelId, $cached)) {
                $novel = $this->novel_model->getNovelInfo_by_id($novelId);
                $cached['n_' . $novelId] = $novel;
            } else
                $novel = $cached['n_' . $novelId];
            $n['name'] = $novel->name;
            $n['slug'] = $novel->slug;
        }
        
        // Get novel by cate
        $novel_1 = $this->novel_model->getNovel_by_category(4);
        $novel_2 = $this->novel_model->getNovel_by_category(3);
        $novel_3 = $this->novel_model->getNovel_by_category(6);
        $listCats = array('cats1' => $novel_1, 'cats2' => $novel_2, 'cats3' => $novel_3);
        
        $this->load->view('header', array('menu_home' => true, 'pageName' => 'pageHome'));
        $this->load->view('home', array('topNovel' => $topNovel, 'update' => $update, 'listCats' => $listCats));
        $this->load->view('sidebar');
        $this->load->view('footer');
    }
    
    function get_all_novel() {
        $allNovel = $this->novel_model->getNovel_all();
        
        foreach ($allNovel as &$novel) {
            $novel->chapter = $this->novel_model->getNovelChapter_by_id($novel->id);
        }
        $this->load->view('header', array('menu_list' => true, 'pageName' => 'pageList'));
        $this->load->view('list', array('allNovel' => $allNovel));
        $this->load->view('sidebar');
        $this->load->view('footer');
    }
}
